﻿using System.Text.RegularExpressions;
using HtmlAgilityPack;

class Program
{
    private static readonly HttpClient client = new HttpClient();
    private static readonly Regex phoneRegex = new Regex(@"\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}", RegexOptions.Compiled);
    private static HashSet<string> visitedUrls = new HashSet<string>();
    private static List<string> extractedPhones = new List<string>();

    static async Task Main(string[] args)
    {
        Console.WriteLine("请输入搜索关键字：");
        string keyword = Console.ReadLine();

        // 使用搜索引擎进行搜索
        string searchUrl = $"https://www.baidu.com/s?wd={keyword}";
        Console.WriteLine($"正在搜索：{searchUrl}");

        // 获取搜索结果
        var searchResults = await GetSearchResultsAsync(searchUrl);

        // 执行异步任务并发爬取每个搜索结果的页面
        var tasks = new List<Task>();
        foreach (var url in searchResults)
        {
            if (!visitedUrls.Contains(url))
            {
                tasks.Add(Task.Run(() => CrawlPageAsync(url)));
                visitedUrls.Add(url);
            }
        }

        await Task.WhenAll(tasks);  // 等待所有任务完成

        // 输出爬取到的URL和电话号码
        Console.WriteLine("\n已爬取的有效URL和电话号码：");
        foreach (var phone in extractedPhones)
        {
            Console.WriteLine(phone);
        }
    }

    // 获取搜索结果URL（简化，实际上你需要解析HTML来获取搜索链接）
    static async Task<List<string>> GetSearchResultsAsync(string searchUrl)
    {
        List<string> urls = new List<string>();
        var response = await client.GetStringAsync(searchUrl);

        // 使用HtmlAgilityPack解析页面，提取搜索结果中的URL
        var doc = new HtmlDocument();
        doc.LoadHtml(response);

        // 提取所有的URL（这里只是示例，实际的搜索引擎结果可能需要更复杂的处理）
        var nodes = doc.DocumentNode.SelectNodes("//h3/a");
        foreach (var node in nodes)
        {
            string url = node.GetAttributeValue("href", "");
            if (url.Contains("http"))
            {
                urls.Add(url);
            }
        }

        return urls;
    }

    // 爬取页面并提取电话号码
    static async Task CrawlPageAsync(string url)
    {
        try
        {
            var pageContent = await client.GetStringAsync(url);

            // 使用正则表达式提取电话号码
            var matches = phoneRegex.Matches(pageContent);
            foreach (Match match in matches)
            {
                if (!extractedPhones.Contains(match.Value))
                {
                    extractedPhones.Add($"{match.Value} (来自: {url})");
                }
            }

            // 控制访问频率，避免过快请求
            await Task.Delay(1000); // 延时1秒，模拟间隔请求
        }
        catch (Exception ex)
        {
            Console.WriteLine($"错误: {ex.Message} - 跳过 URL: {url}");
        }
    }
}
